package com.lw.udpchat;



import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * @author LW
 * @2020/9/16 10:49
 */
public class UDPReceiveDemo01 {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(6666);



        while (true){
            //准备接收包裹
            byte[] buffer = new byte[1024];
            DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);

            socket.receive(packet);//阻塞式接收包裹
            byte[] data = packet.getData();
            String receivedatas = new String(data, 0, data.length);

            System.out.println(receivedatas);
            if(receivedatas.equals("bye")){
                //接收单因为数据长度的问题，equals比较结果为false，连接不会断开
                break;
            }


        }

        socket.close();

    }
}
